package com.runfast.waimai.web.controller.Driver_controller;





import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.runfast.common.Constants;
import com.runfast.common.exception.BaseException;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.paotui.dao.model.RunfastLoginRecord;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.RunfastLoginRecordService;
import com.runfast.paotui.service.RunfastShopperService;
import com.runfast.waimai.dao.model.RunfastBusinessAccounts;
import com.runfast.waimai.dao.model.RunfastShopperExample;
import com.runfast.waimai.util.AESUtil;
import com.runfast.waimai.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class DriverBaseController {
	@Resource
	RunfastLoginRecordService runfastLoginRecordService;
	@Resource
	RunfastShopperService runfastShopperService;
	@Resource
	RedisTemplate redisTemplate;
	/**
	 *   获取登录配送员信息
	 * */

	/*public RunfastShopper getLoginShopper(String token){  //新建的一张表

//		return (RunfastShopper) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
//				.getRequest().getSession().getAttribute(Constant.APP_DRIVER_SESSION);

		String a="abcdefghijklmn";

		Integer	id=Integer.parseInt(AESUtil.decrypt(token,a));
		RunfastShopper runfastShopper=runfastShopperService.get(id);

		*//*if(redisTemplate.opsForValue().get(token)==null){
			throw new BaseException(ResultCode.FAIL,"登录超时，请重新登录");
			//throw new BaseException("登录超时，请重新登录");
		}*//*

		return runfastShopper;
	}*/


	/**
	 *   获取登录配送员信息
	 * */
	public RunfastShopper getLoginShopper(String token){  //新建的一张表

		RunfastShopper shopper = (RunfastShopper) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest().getSession().getAttribute(Constant.APP_DRIVER_SESSION);
		if (shopper == null) {
			String token1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
					.getRequest().getHeader("token");

			//System.out.println(token1+"=============================================================================================================");

			String a="abcdefghijklmn";
			Integer	id=null;
			if(token1==null){
				id=Integer.parseInt(AESUtil.decrypt(token,a));

			}else{
				id=Integer.parseInt(AESUtil.decrypt(token1,a));
			}
			RunfastShopper runfastShopper=runfastShopperService.get(id);

			return runfastShopper;
		}else{
			RunfastShopper runfastShopper=runfastShopperService.selectByPrimaryKey(shopper.getId());
			//System.out.println("-----------从这里获得");
			return runfastShopper;
		}
		//return shopper;

}
	
	
}
